<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/3/21
 * Time: 下午2:49
 */
class GoldService extends Service
{
    public static function indexHeader(){
        $header = array(
            'RecDate'       => array('text' => PubFunc::t('日期')),
            'FlowTypeName'  => array('text' => PubFunc::t('途径')),
            'ActionName'    => array('text' => PubFunc::t('动作')),
            'ActionCoin'    => array('text' => PubFunc::t('货币数量'),'rule'=>'numeric'),
            'ActionUser'    => array('text' => PubFunc::t('人数'),'rule'=>'numeric'),
            'ActionCnt'     => array('text' => PubFunc::t('人次'),'rule'=>'numeric'),
            'ActionRate'   => array('text' => PubFunc::t('比例'),'rule'=>'percent'),
        );

        return $header;
    }

    public static function indexData($params){
        $model = new ModGameGold();
        $data = $model->indexData($params);

        $summary = 0;
        foreach($data as $v){
            $summary += $v['ActionCoin'];
        }

        $flowTypeDict = PubDict::getDict($params['g'], 'FlowType');
        $actionTypeDict = PubDict::getDict($params['g'], 'CoinAction');

        foreach($data as &$v){
            $v['FlowTypeName']  = $flowTypeDict[$v['FlowType']];
            $v['ActionName']    = isset($actionTypeDict[$v['Action']]) ? $actionTypeDict[$v['Action']] : $v['Action'];
            $v['ActionRate']    = number_format($v['ActionCoin'] / $summary * 100, 2) . '%';
        }

        return $data;
    }

    public static function indexChart($data, $params){
        $chartDataGet = $chartDataUsed = array();

        $coinTypeDict = PubDict::getDict($params['g'], 'CoinType');
        $flowTypeDict = PubDict::getDict($params['g'], 'FlowType');

        $coinType = $coinTypeDict[$params['coinType']];
        $flowType = $flowTypeDict[$params['flowType']];

        $label = PubFunc::t($coinType . $flowType);

        $pubSort = new PubMultiSort();
        $pubSort->setSort('ActionCoin','desc');
        $data = $pubSort->getResult($data);

        $i = 20;
        foreach($data as $v){
            $chartData[$label][PubFunc::t($v['ActionName'])] = intval($v['ActionCoin']);

            if(--$i < 1){
                break;
            }
        }

        return array(
            'data'      => $chartData,
            'config'    =>  array(
                'name'   => $label,
                'type'   => 'bar',
                'config'=>array(
                    'xAxis' => array('axisLabel' => array('rotate' => 40)),
                    'normal'=>array(
                        'grid'=>array(
                            "x" => 100
                        ),
                    ),
                ),
            )
        );
    }

    public static function analysisInfo($params){
        $dictAction = PubDict::dict($params['g'], 'itemaction');

        $tmp    = ModGameGold::analysisData($params);

        $useActions = $getActions = array();
        foreach($tmp as $v){
            if($v['Action'] != 'ALL' && !isset($actions[$v['Action']])){
                if(empty($v['UsedNumber'])){
                    $getActions[$v['Action']] = array('text' => $dictAction[$v['Action']] . '-' . $v['Action'], 'rule'=>'numeric');
                }else{
                    $useActions[$v['Action']] = array('text' => $dictAction[$v['Action']] . '-' . $v['Action'], 'rule'=>'numeric');
                }
            }
        }

        $header = array(
            'RecDate'   => array('text' => '日期'),
            'Get'       => array(
                'text'      => '产出',
                'include'   => $getActions + array(
                    'GetTotal' => array('text' => '合计', 'rule'=>'numeric')
                )
            ),
            'Use'       => array(
                'text'      => '消耗',
                'include'   => $useActions + array(
                    'UseTotal' => array('text' => '合计', 'rule'=>'numeric')
                )
            ),
            'Rate'      => array('text' => '消耗占比', 'rule'=>'numeric')
        );

        $data   = array();
        foreach($tmp as $v){
            $col = $v['Action'];

            if(!isset($data[$v['RecDate']])){
                $data[$v['RecDate']] = array(
                    'RecDate' => $v['RecDate']
                );
            }

            $data[$v['RecDate']][$col] = $v['Action'];
        }

        $total = array('RecDate' => '合计');
        foreach($data as &$v){
            foreach($getActions as $ik=>$iv){
                if(!isset($v[$ik])){
                    $v[$ik] = '--';
                    $v['GetTotal'] += 0;
                }else{
                    $total[$ik] += $v[$ik];
                    $v['GetTotal'] += $v[$ik];
                }
            }
            foreach($useActions as $ik=>$iv){
                if(!isset($v[$ik])){
                    $v[$ik] = '--';
                    $v['UseTotal'] += 0;
                }else{
                    $total[$ik] += $v[$ik];
                    $v['UseTotal'] += $v[$ik];
                }
            }
            $v['Rate'] = number_format($v['UseTotal'] / $v['GetTotal'] * 100, 2) . '%';

            $total['GetTotal'] += $v['GetTotal'];
            $total['UseTotal'] += $v['UseTotal'];
        }
        unset($v);

        $total['Rate'] = number_format($total['UseTotal'] / $total['GetTotal'] * 100, 2) . '%';

        return array(
            'header' => $header,
            'data'   => array(
                'data'  => $data,
                'total' => $total
            )
        );
    }
}